n,d = map(int, input().split())
a = input().split()
a = [int(el) for el in a]
l = 0
cnt = 0
for r in range(n):
while l<n and (a[r] - a[l] > d):
l += 1
pointes_on_segment_cnt = r - l
cnt += pointes_on_segment_cnt*(pointes_on_segment_cnt-1)//2
print(cnt)
#include<bits/stdc++.h>
using namespace std;
#define INF 1000000007
#define INF_ll 1000000000000000007
#define ll long long
#define endl "\n"
#define uint unsigned long long
#define unmp unordered_map
#define unst unordered_set
#define int long long
#define f first
#define s second
#define watch(x) cerr << "\n" << (#x) << " is " << (x) << endl
#define line cout << " " << endl
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define safai(...) Clearing_out(__VA_ARGS__)
template <typename A, typename B>
string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) {
return '"' + s + '"';
}
string to_string(char c) {
string s;
s += c;
return s;
}
string to_string(const char* s) {
return to_string((string) s);
}
string to_string(bool b) {
return (b ? "1" : "0");
}
string to_string(vector<bool> v) {
bool first = true;
string res = "{";
for (int i = 0; i < static_cast<int>(v.size()); i++) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(v[i]);
}
res += "}";
return res;
}
template <size_t N>
string to_string(bitset<N> v) {
string res = "";
for (size_t i = 0; i < N; i++) {
res += static_cast<char>('0' + v[i]);
}
return res;
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
void Clearing_out() { return; }
template <typename Head, typename... Tail>
void Clearing_out(Head &H, Tail & ... T) {
H.clear();
Clearing_out(T...);
}
void time(){
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
}
void hi(){
static int i=0;
cerr << "Check Point : " << ++i << endl;
return;
}
/***************************************************/
/////////////////////////////////////////////////////
int stoi1(string str){
bool negative;
if(str[0]=='-'){
negative=true;
str=str.substr(1);
}
else{
negative=false;
}
int n=str.length();
int number = 0;
int pos = 1;
for(int i=n-1;i>=0;i--){
number = number + (str[i]-'0')*pos;
pos=pos*10;
}
if(negative) number=-number;
return number;
}
int power(int a,int n){
int res = 1;
while(n>0){
if(n%2!=0){
res = res*a;
n--;
}
else if(n%2==0){
a = a*a;
n=n/2;
}
}
return res;
}
/////////////////////////////////////////////////////
//DECLARE VARIABLE AND MAKE CHANGES AFTER HERE
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
t=1;
//cin >> t;
while(t--){
int n, d;
cin >> n >> d;
vector<int> v(n);
for(int i = 0;i<n;i++){
cin >> v[i];
}
int ans = 0;
for(int i = 0;i<n;i++){
int index = lower_bound(v.begin(), v.end(), v[i]+d) - v.begin();
if(index==n){
index = n-1;
}
else{
if(v[index]>v[i]+d){
index--;
}
else{
}
}
if(index<=i) continue;
ans += ((index-i-1)*(index-i))/2;
}
cout << ans << endl;
}
time();
return 0;
}
1475E - Advertising Agency | 1345B - Card Constructions |
1077B - Disturbed People | 653A - Bear and Three Balls |
794A - Bank Robbery | 157A - Game Outcome |
3B - Lorry | 1392A - Omkar and Password |
489A - SwapSort | 932A - Palindromic Supersequence |
433A - Kitahara Haruki's Gift | 672A - Summer Camp |
1277A - Happy Birthday Polycarp | 577A - Multiplication Table |
817C - Really Big Numbers | 1355A - Sequence with Digits |
977B - Two-gram | 993A - Two Squares |
1659D - Reverse Sort Sum | 1659A - Red Versus Blue |
1659B - Bit Flipping | 1480B - The Great Hero |
1519B - The Cake Is a Lie | 1659C - Line Empire |
515A - Drazil and Date | 1084B - Kvass and the Fair Nut |
1101A - Minimum Integer | 985D - Sand Fortress |
1279A - New Year Garland | 1279B - Verse For Santa |